#include <unistd.h>
#include "global_const.h"
#include "administrator.h"
#include "lab_reservation.h"

// 静态变量初始化 一定要在cc文件中哦 .h文件中是变量申明
string Administrator::m_def_account = "admin";
string Administrator::m_def_pwd = "admin";

Administrator::Administrator()
{
}

Administrator::Administrator(string name, string password)
{
    m_name = name;
    m_password = password;

    // 初始化机房
    init_lab();
}

Administrator::~Administrator()
{
}

void Administrator::menu()
{
    cout << "\t\t ---------------------------------\n";
    cout << "\t\t|                                |\n";
    cout << "\t\t|          1.添加账号            |\n";
    cout << "\t\t|                                |\n";
    cout << "\t\t|          2.查看账号            |\n";
    cout << "\t\t|                                |\n";
    cout << "\t\t|          3.查看机房            |\n";
    cout << "\t\t|                                |\n";
    cout << "\t\t|          4.清空预约            |\n";
    cout << "\t\t|                                |\n";
    cout << "\t\t|          0.注销登录            |\n";
    cout << "\t\t|                                |\n";
    cout << "\t\t ---------------------------------\n";
    cout << "请选择您的操作： " << endl;
}

void Administrator::action(int choice)
{
    switch (choice)
    {
    case 1: // 添加账号
        add_account();
        break;
    case 2: // 查看账号
    {
        search_account();
        break;
    }
    case 3: // 查看机房
    {
        search_lab();
        break;
    }
    case 4: // 清空预约
    {
        clear_all_rsvt();
        break;
    }
    default:
        cout << "选择错误" << endl;
        break;
    }
}

void Administrator::add_account()
{
    cout << "请选择添加的身份： (1-学生 2-教师)" << endl;
    string filename = "";
    int choice;
    cin >> choice;
    if (choice == 1)
    {
        filename = STUDENT_FILE;
        cout << STUDENT_TIPS << endl;
    }
    else
    {
        filename = TEACHER_FILE;
        cout << TEACHER_TIPS << endl;
    }

    // 获取用户输入
    vector<string> input = get_input();

    // 添加账户
    // 检查账户是否存在
    ifstream ifs(filename, ios::in);
    string id;
    string name;
    string pwd;
    while (ifs >> id && ifs >> name && ifs >> pwd)
    {
        cout << id << name << pwd << endl;
        if (input[0] == id)
        {
            cout << "账户已存在" << endl;
            return;
        }
    }
    ifs.close();

    // 添加
    ofstream ofs(filename, ios::out | ios::app);
    ofs << input[0] << " " << input[1] << " " << input[2] << endl;
    ofs.close();
    cout << "添加成功" << endl;
}

void Administrator::search_account()
{
    cout << "请选择查看的身份： (1-学生 2-教师)" << endl;
    int choice;
    cin >> choice;
    // string filename = choice == 1 ? STUDENT_FILE : TEACHER_FILE;
    string filename;
    string id_title;
    if (choice == 1)
    {
        filename = STUDENT_FILE;
        id_title = "学号";
    }
    else
    {
        filename = TEACHER_FILE;
        id_title = "工号";
    }
    ifstream ifs(filename);
    if (!ifs.is_open())
    {
        cout << "-------------------------" << endl;
        cout << "无记录" << endl;
        cout << "-------------------------" << endl;
        return;
    }

    string id;
    string name;
    string pwd;
    cout << "-------------------------" << endl;
    while (ifs >> id && ifs >> name && ifs >> pwd)
    {
        cout << id_title << "：" << id << " 姓名：" << name << " 密码：" << pwd << endl;
    }
    cout << "-------------------------" << endl;
    ifs.close();
}

void Administrator::search_lab()
{
    show_lab();
}

void Administrator::clear_all_rsvt()
{
    ofstream ofs(TEACHER_FILE, ios::trunc);
    ofs.close();
    cout << "预约记录已清除" << endl;
}